﻿-- Author      : Kurapica

--------------------------
-- WidgetList	        --
--------------------------
do
	local getmetatable = getmetatable
	local pairs = pairs
	local ipairs = ipairs
	local type = type
	local error = error
	local CreateFrame = IGAS.GUI.CreateFrame
    
	local _WidgetName = "WidgetList"
	local _Base = IGAS.GUI.Widget.Frame

	local _Widget = IGAS.Studio.Widget
	local _GUIWidget = IGAS.GUI.Widget
	local _PropertyType = IGAS.GUI.Base.PropertyType

	-- Script
	local _FrameBackdrop = {
		bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		tile = true, tileSize = 16, edgeSize = 16,
		insets = { left = 3, right = 3, top = 3, bottom = 3 }
	}
	
	local function OnMouseDown(self)
		self:StartMoving()
	end

	local function OnMouseUp(self)
		self:StopMovingOrSizing()
	end
	
    local function sizersOnMouseDown(self)
		if self:GetParent().Resizable then
			self:GetParent():StartSizing("BOTTOM")
		end
	end
	
	local function sizerOnMouseUp(self)
		self:GetParent():StopMovingOrSizing()
	end
	
	-- ScriptType
	local _ScriptType = {
		["OnWidgetSelected"] = true,
	}

	--	FuncProxy
	local _FuncProxy = {
		-- Dispose, release resource
        ["Dispose"] = function(self)			
			-- Dispose Childs
			if self.__WidgetSeparatorNum and self.__WidgetSeparatorNum > 0 then
				for i = self.__WidgetSeparatorNum, 1, -1 do
					if self:GetChild("WidgetSeparator"..i) then
						self:GetChild("WidgetSeparator"..i):Dispose()
					end
				end
			end
			
            -- Call super's dispose
			self:CallMethod(_Base, "Dispose")
        end,
		
		-- AddWidget
		["AddWidget"] = function(self, group, widget)
			local container = self.Container
						
			if not group or type(group) ~= "string" or not widget then
				return
			end
						
			if type(widget) == "string" then
                if _GUIWidget[widget] then
                    widget = _GUIWidget[widget]
				else
					return
                end
            elseif type(widget) == "table" then
                if not widget["FuncProxy"] then
                    return
                end
            else
                return
            end

			if container.__WidgetSeparatorNum and container.__WidgetSeparatorNum > 0 then
				for i = 1, container.__WidgetSeparatorNum do
					if container:GetChild("WidgetSeparator"..i).Text == group then						
						return container:GetChild("WidgetSeparator"..i):AddWidget(widget)
					end
				end
			end
			
			local separator = CreateFrame(_Widget["WidgetSeparator"], nil, container)
			separator.Text = group
			
			return separator:AddWidget(widget)
		end,
		-- RemoveWidget
		["RemoveWidget"] = function(self, group, widget)
			local container = self.Container
			
			if not group or type(group) ~= "string" or not widget then
				return
			end
			
			if type(widget) == "string" then
                if _GUIWidget[widget] then
                    widget = _GUIWidget[widget]
				else
					return
                end
            elseif type(widget) == "table" then
                if not widget["FuncProxy"] then
                    return
                end
            else
                return
            end
			
			if container.__WidgetSeparatorNum and container.__WidgetSeparatorNum > 0 then
				for i = 1, container.__WidgetSeparatorNum do
					if container:GetChild("WidgetSeparator"..i).Text == group then
						container:GetChild("WidgetSeparator"..i):RemoveWidget(widget)
						return
					end
				end
			end
		end,
		-- RemoveGroup
		["RemoveGroup"] = function(self, group)
			local container = self.Container
			
			if not group or type(group) ~= "string" then
				return
			end
			
			if container.__WidgetSeparatorNum and container.__WidgetSeparatorNum > 0 then
				for i = 1, container.__WidgetSeparatorNum do
					if container:GetChild("WidgetSeparator"..i).Text == group then
						container:GetChild("WidgetSeparator"..i):Dispose()
						return
					end
				end
			end
		end,
		-- GetGroup
		["GetGroup"] = function(self, group)
			local container = self.Container
			
			if not group or type(group) ~= "string" then
				return
			end
			
			if container.__WidgetSeparatorNum and container.__WidgetSeparatorNum > 0 then
				for i = 1, container.__WidgetSeparatorNum do
					if container:GetChild("WidgetSeparator"..i).Text == group then
						return container:GetChild("WidgetSeparator"..i)
					end
				end
			end
		end,
		-- DisSelected
		["DisSelected"] = function(self)
			if self.Container.__SelBtn then
				self.Container.__SelBtn:Fire("OnMouseDown")
			end
		end,
	}

	--	Property
	local _Property = {
        -- Container
        ["Container"] = {
			["Get"] = function(self)
				return self.ScrollForm.Container
			end,
			
			["Type"] = _PropertyType.Object,
        },
		-- SelWidget
		["SelectedWidget"] = {
			["Get"] = function(self)
				return self.Container.__SelBtn and self.Container.__SelBtn.Widget
			end,
			
			["Type"] = _PropertyType.Object,
		},
	}

	local function _New(parent)		
		local frame = CreateFrame(_Base, nil, parent)
		
		frame.Width =200
		frame.Height = 400
		
		frame.Movable = true
		frame.Resizable = true
		frame.FrameStrata = "DIALOG"
		frame.Toplevel = true
		frame.MouseEnabled = true
		frame.MouseWheelEnabled = true
		frame.OnMouseDown = OnMouseDown
		frame.OnMouseUp = OnMouseUp
		
		frame:SetPoint("LEFT", parent, "LEFT", 0, 10)
		frame:SetBackdrop(_FrameBackdrop)
		frame:SetBackdropColor(0,0,0,1)
		frame:SetMinResize(200,200)
		frame:SetMaxResize(200,1000)
        
		local sizer_s = CreateFrame("Frame","Sizer_s",frame)
		sizer_s:SetPoint("BOTTOMRIGHT",frame,"BOTTOMRIGHT",0,0)
		sizer_s:SetPoint("BOTTOMLEFT",frame,"BOTTOMLEFT",0,0)
		sizer_s.Height = 10
		sizer_s.MouseEnabled = true
		sizer_s.OnMouseDown = sizersOnMouseDown
		sizer_s.OnMouseUp = sizerOnMouseUp
		
		local scrollForm = CreateFrame("ScrollForm", "ScrollForm", frame)
		scrollForm:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, -10)
		scrollForm:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 0, 10)
		scrollForm:SetBackdrop(nil)
		scrollForm.ValueStep = 26
		scrollForm.Container.Height = 26
		
		return frame
	end	

	-- Register
	local _WidgetInfo = {
		["WidgetName"] = _WidgetName,
		["Base"] = _Base,
		["FuncProxy"] = _FuncProxy,
		["Property"] = _Property,
		["ScriptType"] = _ScriptType,
		["New"] = _New,
	}

	_Widget[_WidgetName] = IGAS:NewWidget(_WidgetInfo)
end
